package nl.boektrust.btsoep.shared.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;


import com.googlecode.objectify.annotation.Serialized;


/**
 * 
 */

/**
 * @author rao3
 *
 */
@Entity
public class BTEntity implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id Long id;
	private String name;
	private String url;
	@Serialized private ArrayList<Identifier> identifiers;
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}
	
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * @param url the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}
	
	/**
	 * @param name
	 * @param url
	 * @param personId
	 */
	public BTEntity(String name, String url) {
		super();
		this.name = name;
		this.url = url;
		identifiers = new ArrayList<Identifier>();
	}
	
	public void addIdentifier(Identifier id) {
		identifiers.add(id);
		System.out.println("BTEntity:addIdentifier: Adding identifier (type: "+id.getType()+" value: "+id.getValue()+")");
		System.out.println("BTEntity:addIdentifier: Identifiers now has length: "+identifiers.size());
	}
	
	public void removeIdentifier(Identifier id) {
		for (Identifier id2 : identifiers) {
			if (id2.getValue().equals(id.getValue()) && id.getType() == id2.getType()) {
				identifiers.remove(id2);
			}
		}
	}
	
	public ArrayList<Identifier> getIdentifiers () {
		return identifiers;
	}
	
	public BTEntity() {
		this("", "");
	}
	
	public String lastPartOfName() {
		List<String> stringsList = new ArrayList<String>(Arrays.asList(this.name.split(" +")));
//		String[] strings = this.name.split(" +");
		return stringsList.get(stringsList.size()-1);
		
	}
	
	public String viafToString() {
		String resultstring = new String("");
		for (Identifier id: identifiers) {
			if (id.getType() == 1) {
				if (resultstring.length() > 0) {
					resultstring = resultstring + "," + id.getValue();
				} else {
					resultstring = id.getValue();
				}
			}
		}
		return resultstring;
	}
	
	public boolean parseViafString(String str) {
		System.out.println("BTEntity:parseViafString: Trying to parse string "+str);
		boolean valid_viaf = true;

		try {
			String[] substrings = str.split(",");
			for (String st : substrings) {
				System.out.println("substring: "+ st);
				for (int i=0; i<st.length(); i++) {
					char ch = st.charAt(i);
					if (Character.isDigit(ch) == false) {
						valid_viaf = false;		
						System.out.println("found false character in string "+st+": "+ch);
					}
				}
			}
			// if we found at least one non-viaf character, return false.
			if (valid_viaf == false) return false;
			for (String st : substrings) {
				this.addIdentifier(new Identifier(1, st));
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean isViafStringParsable(String str) {
		System.out.println("BTEntity:parseViafString: Trying to parse string "+str);
		boolean valid_viaf = true;

		try {
			String[] substrings = str.split(",");
			for (String st : substrings) {
				System.out.println("substring: "+ st);
				for (int i=0; i<st.length(); i++) {
					char ch = st.charAt(i);
					if (Character.isDigit(ch) == false) {
						valid_viaf = false;		
						System.out.println("found false character in string "+st+": "+ch);
					}
				}
			}
			// if we found at least one non-viaf character, return false.
			if (valid_viaf == true) return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}


}


